

#include <DFRobot_BC20_Gravity.h>


DFRobot_BC20_IIC myBC20(0x33);


void ConnectCloud() {
  Serial.print("Attempting MQTT connection...");
  myBC20.changeColor(YELLOW);
  while (!myBC20.connected()) {
    Serial.print(".");
    myBC20.LED_ON();
    delay(500);
    myBC20.LED_OFF();
    delay(500);

    if (myBC20.connect("0",  "agZeZ1B4g", "aRWeWJf4gz")) {
      Serial.println("\nConnect Server OK");
    } else {
      /**
         Used to detect the connection between the device and the server
      */
      if (myBC20.getQMTCONN())
        break;
    }
  }
}

void setup() {
  Serial.begin(9600);
  delay(3000);
  myBC20.LED_OFF();

  /*Initialize BC20*/
  Serial.print("Starting the BC20.Please wait. . . ");
  myBC20.changeColor(RED);
  while (!myBC20.powerOn()) {
    Serial.print(".");
    myBC20.LED_ON();
    delay(500);
    myBC20.LED_OFF();
    delay(500);
  }
  Serial.println("BC20 started successfully!");

  /* Disable sleep mode */
  myBC20.configSleepMode(eSleepMode_Disable);

  /*Power up GNSS*/
  Serial.print("Turning on GNSS ... ");
  myBC20.setQGNSSC(ON);
  myBC20.changeColor(YELLOW);
  if (myBC20.getQGNSSC() == OFF) {
    Serial.print(".");
    myBC20.LED_ON();
    delay(500);
    myBC20.LED_OFF();
    delay(500);
  }
  Serial.println("GNSS is ON.");
  myBC20.changeColor(CYAN);
  while (true){
    
  myBC20.getQGNSSRD2();
  //Display_Location_Information();
  Serial.print(sGGNS2.LatitudeVal,6);
  Serial.print(",");
  Serial.println(sGGNS2.LongitudeVal,6);
  Serial.println("----");
  myBC20.clearGPS();
  if (sGGNS2.LatitudeVal>0){
    break;
  }
  
  delay(1000);
  }
  
myBC20.setQGNSSC(OFF);
delay(3000);
  Serial.println("Connecting to DFRobot Easy-IoT");

  //Configure IoT Server
  myBC20.setServer("iot.dfrobot.com.cn", "1883");
  Serial.println("Server is available!");
  ConnectCloud();
  
  
  
  Serial.println("send message to cloud...");
  //myBC20.publish("zMh8dYYVR", "hello");
  myBC20.publish("zMh8dYYVR", (String(sGGNS2.LatitudeVal)+","+String(sGGNS2.LongitudeVal)));
  
  Serial.println("Message is sent.");
}

void loop() {
}